#!/bin/sh

# AM INSTALL SCRIPT VERSION 3.5
set -u
APP=speedcrunch
SITE1=$(curl -Ls https://api.bitbucket.org/2.0/repositories/heldercorreia/speedcrunch/downloads | sed 's/[()",{} ]/\n/g' | grep -oi "https.*$APP.*64.*bz2$" | head -1)
SITE2=$(curl -Ls https://api.github.com/repos/Samueru-sama/SpeedCrunch-AppImage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*$APP.*appimage$" | head -1)

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/tmp" "/opt/$APP/icons" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > ../remove
printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> ../remove
chmod a+x ../remove || exit 1

# CHOOSE A VERSION
read -r -p "
 Choose which version of Ryujinx to use:
 
 1. SpeedCrunch
	Official portable build of SpeedCrunch
	Source: https://ryujinx.org

 2. SpeedCrunch AppImage
	Unofficial AppImage of SpeedCrunch, has the advantage that it is one smaller file
	It can be sandboxed with 'am --sandbox speedcrunch' or 'appman --sandbox speedcrunch'
	Source: https://github.com/Samueru-sama/SpeedCrunch-AppImage

 Which version you choose (type a number and press ENTER)?: " RESPONSE
case "$RESPONSE" in
	1) rm -f /opt/"$APP"/version
	UPDATER=1
	version="$SITE1"
	wget "$version" || exit 1;;

	2) rm -f /opt/"$APP"/version
	UPDATER=2
	version="$SITE2"
	wget "$version" || exit 1;;

	*) ../remove && echo "Not a valid number, aborting" && exit 1;;
esac

# PREPARE THE APP, $version is also used for updates
[ -e ./*tar.* ] && tar fx ./*tar.* && rm -f ./*tar.*
cd ..
if [ -e ./tmp/*mage ]; then mv ./tmp/*mage ./"$APP"; else mv ./tmp/"$APP"* ./"$APP"; fi
rm -R -f ./tmp || exit 1
echo "$version" > ./version
chmod a+x ./"$APP" || exit 1
ln -s "/opt/$APP/$APP" "/usr/local/bin/$APP" # LINK TO PATH

# SCRIPT TO UPDATE THE PROGRAM
cat >> ./AM-updater << 'EOF'
#!/bin/sh
set -u
APP=speedcrunch
#SITE1=$(curl -Ls https://api.bitbucket.org/2.0/repositories/heldercorreia/speedcrunch/downloads | sed 's/[()",{} ]/\n/g' | grep -oi "https.*$APP.*64.*bz2$" | head -1)
#SITE2=$(curl -Ls https://api.github.com/repos/Samueru-sama/SpeedCrunch-AppImage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*$APP.*appimage$" | head -1)
version0=$(cat "/opt/$APP/version")
version="REPLACETHIS"
[ -n "$version" ] || { echo "Error getting link"; exit 1; }
if [ "$version" != "$version0" ]; then
	mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	notify-send "A new version of $APP is available, please wait"
	wget "$version" || exit 1
	[ -e ./*tar.* ] && tar fx ./*tar.* && rm -f ./*tar.*
	cd ..
	if [ -e ./tmp/*mage ]; then mv --backup=t ./tmp/*mage ./"$APP"; else mv --backup=t ./tmp/"$APP"* ./"$APP"; fi
	chmod a+x ./"$APP" || exit 1
	echo "$version" > ./version
	rm -R -f ./tmp ./*~
	notify-send "$APP is updated!"
else
	echo "Update not needed!"
fi
EOF
[ "$UPDATER" = 1 ] && { sed -i 's|REPLACETHIS|$SITE1|g; s|#SITE1|SITE1|g' ./AM-updater || exit 1; }
[ "$UPDATER" = 2 ] && { sed -i 's|REPLACETHIS|$SITE2|g; s|#SITE2|SITE2|g' ./AM-updater || exit 1; }
chmod a+x ./AM-updater || exit 1
# LAUNCHER & ICON
if [ "$UPDATER" = "1" ]; then
	DESKTOP="https://bitbucket.org/heldercorreia/speedcrunch/raw/fa4f5d23f28b6458b54c617230f66af41fc94d7e/pkg/org.speedcrunch.SpeedCrunch.desktop"
	ICON="https://bitbucket.org/heldercorreia/speedcrunch/raw/fa4f5d23f28b6458b54c617230f66af41fc94d7e/gfx/speedcrunch.svg"
	wget -q "$DESKTOP" -O ./"$APP".desktop && wget -q "$ICON" -O ./DirIcon
else
	./"$APP" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$APP".desktop
	./"$APP" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
	COUNT=0
	while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
		if [ -L ./"$APP".desktop ]; then
			LINKPATH="$(readlink ./"$APP".desktop | sed 's|^\./||')"
			./"$APP" --appimage-extract "$LINKPATH" 1>/dev/null && mv ./squashfs-root/"$LINKPATH" ./"$APP".desktop
		fi
		if [ -L ./DirIcon ]; then
			LINKPATH="$(readlink ./DirIcon | sed 's|^\./||')"
			./"$APP" --appimage-extract "$LINKPATH" 1>/dev/null && mv ./squashfs-root/"$LINKPATH" ./DirIcon
		fi
		[ ! -L ./"$APP".desktop ] && [ ! -L ./DirIcon ] && break
		COUNT=$((COUNT + 1))
	done
	rm -R -f ./squashfs-root
fi
sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g" ./"$APP".desktop
mv ./"$APP".desktop /usr/local/share/applications/"$APP"-AM.desktop && mv ./DirIcon ./icons/"$APP" 1>/dev/null
